import pandas as pd
import numpy as np
import datetime
import os

from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import VotingClassifier

from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier

cur_time = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")

# # 获取当前脚本的目录路径
project_dir = os.path.dirname(os.path.abspath(__file__))

# df = pd.DataFrame(data)
df = pd.read_csv(os.path.join(project_dir,'data/train_prob_04_2024-01-02_15:43:45.csv'))
data = df.to_numpy()
x = data[:,1:]
y = data[:,0]

# X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)
X_train = x
y_train = y

clf = MultinomialNB(alpha=0.02)
sgd_model = SGDClassifier(max_iter=8000, tol=1e-4, loss="modified_huber") 
p6={'n_iter': 2500,'verbose': -1,'objective': 'cross_entropy','metric': 'auc',
    'learning_rate': 0.00581909898961407, 'colsample_bytree': 0.78,
    'colsample_bynode': 0.8, 'lambda_l1': 4.562963348932286, 
    'lambda_l2': 2.97485, 'min_data_in_leaf': 115, 'max_depth': 23, 'max_bin': 898}
lgb=LGBMClassifier(**p6)
cat=CatBoostClassifier(iterations=2500,
                       verbose=0,
                       l2_leaf_reg=6.6591278779517808,
                       learning_rate=0.005599066836106983,
                       subsample = 0.4,
                       allow_const_label=True,loss_function = 'CrossEntropy')
weights = [0.07,0.31,0.31,0.31]

ensemble = VotingClassifier(estimators=[('mnb',clf),
                                        ('sgd', sgd_model),
                                        ('lgb',lgb), 
                                        ('cat', cat)
                                       ],
                            weights=weights, voting='soft', n_jobs=-1)
ensemble.fit(X_train, y_train)
# ans = ensemble.score(X_test,y_test)
# print(ans)
# df = pd.read_csv(os.path.join(project_dir,'data/train_prob_04_2024-01-02_19:06:38.csv'))
df = pd.read_csv(os.path.join(project_dir,'data/train_prob_04_2024-01-02_20:44:46.csv'))
data = df.to_numpy()
X_test = data[:,1:]
y_test = data[:,0]

print(y_test)

ans = ensemble.score(X_test,y_test)
df['pred'] = ensemble.predict_proba(X_test)[:,1]
print(df['pred'])
df.to_csv('data/train_prob_04_2024-01-02_19:06:38_pred.csv',index=False)
